#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// 215. 数组中的第K个最大元素
// https://leetcode.cn/problems/kth-largest-element-in-an-array/description/

class Solution
{
public:
    bool isStandard(vector<int> &nums, int curNum, int k)
    {
        int ret = count(nums.begin(), nums.end(), [=](int x)
                        { return x >= curNum; });
        return ret >= k;
    }
    int findKthLargest(vector<int> &nums, int k)
    {
        int right = *max_element(nums.begin(), nums.end());
        int left = *min_element(nums.begin(), nums.end());

        int ans = nums[0];
        while (left <= right)
        {
            int curNum = (right - left) / 2 + left;
            if(isStandard(nums, curNum, k)){
                ans = curNum;
                left = curNum + 1;
            }
            else
            {
                right = curNum - 1;
            }
        }
        return ans;
    }
};

int main()
{
    return 0;
}